package com.tenebrous.jestic.factorys;

import com.tenebrous.jestic.auth.AuthTokenGenerator;
import com.tenebrous.jestic.chain.AbstractJesticChainHandler;
import com.tenebrous.jestic.chain.JesticAuthHandler;
import com.tenebrous.jestic.chain.JesticAuthVerificationHandler;
import com.tenebrous.jestic.context.JesticTask;
import com.tenebrous.jestic.context.Response;
import com.tenebrous.jestic.entity.model.request.JesticAuthorityRequest;
import com.tenebrous.jestic.entity.model.response.JesticAuthorityResponse;
import com.tenebrous.jestic.enums.TaskEnum;
import com.tenebrous.jestic.server.AbstractInstanceContext;
import com.tenebrous.jestic.service.server.AuthServerService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description:
 * @Project: jestic-server
 * @Package: com.tenebrous.jestic.factorys
 * @Author: zhangya
 * @CreateTime: 2023-06-29  14:14
 * @Version: 1.0.0
 */
@Component
public class JesticAuthorityInitHandler<T extends JesticTask, C extends Response> implements AbstractJesticInitInterface<T, C>, InitializingBean {

    @Resource
    private JesticPropertyChainFactory<T, C> jesticPropertyChainFactory;

    @Resource
    private AuthServerService authServerService;

    @Resource
    private AbstractInstanceContext abstractInstanceContext;

    @Resource
    private AuthTokenGenerator authToken;

    @Override
    public AbstractJesticChainHandler<T, C> buildBuilder() {
        AbstractJesticChainHandler.Builder<T, C> builder = new AbstractJesticChainHandler.Builder<>();
        return builder
                .addHandler((AbstractJesticChainHandler<T, C>) new JesticAuthVerificationHandler())
                .addHandler((AbstractJesticChainHandler<T, C>) new JesticAuthHandler(authServerService, abstractInstanceContext, authToken))
                .build();
    }

    @Override
    public TaskEnum getTaskType() {
        return TaskEnum.AUTHORITY;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        AbstractJesticChainHandler<T, C> handler = buildBuilder();
        jesticPropertyChainFactory.register(getTaskType().getCode(), handler);
    }
}
